<template>
    <div class="app-container">
      <!-- 上传操作区 -->
      <div class="filter-container">
        <el-upload
          class="upload-demo"
          method="post"
          :multiple="false"
          :action="upload.url"
          :headers="upload.headers"
          :before-upload="beforeUpload"
          :on-success="handleSuccess"
          :on-error="handleError"
          :show-file-list="false"
          accept=".xlsx, .xls"
        >
          <el-button
            type="primary"
            icon="el-icon-upload"
            size="small"
          >上传Excel文件</el-button>
          <div slot="tip" class="el-upload__tip">支持.xls, .xlsx格式，文件不超过50MB</div>
        </el-upload>
      </div>

      <!-- 结果展示区 -->
      <el-table
        v-loading="loading"
        :data="resultData"
        border
        fit
        highlight-current-row
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column label="输入元素中文名" prop="输入元素中文名" align="center" />
        <el-table-column label="输入元素英文名" prop="输入元素英文名" align="center" />
        <el-table-column label="输入说明" prop="输入说明" align="center" />

        <el-table-column label="审核结果" align="center">
          <template slot-scope="{row}">
            <el-tag
              :type="row['审核结果'] | statusFilter"
              effect="dark"
            >{{ row['审核结果'] }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="相似记录条数" prop="相似记录条数" align="center" />

        <el-table-column label="相似记录" align="center">
          <template slot-scope="{row}">
            <el-popover
              placement="right"
              width="600"
              trigger="click"
            >
              <el-table :data="row['相似记录']">
                <el-table-column property="数据元素中文名" label="中文名" />
                <el-table-column property="数据元素英文名" label="英文名" />
                <el-table-column property="说明" label="说明" />
              </el-table>
              <el-button slot="reference" type="text">查看详情（{{ row['相似记录条数'] }}条）</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </template>

  <script>
  import { getToken } from '@/utils/auth'

  export default {
    name: 'Ysmx',
    filters: {
      statusFilter(status) {
        const statusMap = {
          '通过': 'success',
          '不通过': 'danger',
          '用户未输入元素': 'warning'
        }
        return statusMap[status] || 'info'
      }
    },
    data() {
      return {
        upload: {
          url: process.env.VUE_APP_BASE_API + '/ysmxUpload',
          headers: { Authorization: 'Bearer ' + getToken() }
        },
        resultData: [],
        loading: false
      }
    },
    methods: {
      beforeUpload(file) {
        const isExcel = file.type.includes('spreadsheet') ||
          ['.xls', '.xlsx'].some(ext => file.name.endsWith(ext))
        const isLt50M = file.size / 1024 / 1024 < 50

        if (!isExcel) {
          this.$message.error('请上传Excel文件!')
          return false
        }
        if (!isLt50M) {
          this.$message.error('文件大小不能超过50MB!')
          return false
        }
        return true
      },

      async handleSuccess(response) {
        if (response.code != 200) {
          this.$message.error(response.msg || '解析失败')
          return
        }
        this.resultData = response.data
        this.$message.success('解析成功')
      },
      async handleError(response) {
        if (response.code != 200) {
          this.$message.error(response.msg || '解析失败')
          return
        }
        this.resultData = response.data
        this.$message.success('解析成功')
      }
    }
  }
  </script>

  <style scoped>
  .filter-container {
    padding: 20px;
    background: #f8f9fa;
    border-radius: 4px;
    margin-bottom: 20px;
  }

  .el-upload__tip {
    margin-top: 8px;
    color: #606266;
    font-size: 12px;
  }

  ::v-deep .el-table .cell {
    white-space: nowrap;
  }
  </style>
